#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from typing import Union
from unittest import result
from web_server.models.activities.base_activity import BaseActivity
from web_server.models.ui.uicomponent import UiComponent
from common.utils import LogUtil

logger = LogUtil.get_logger(__name__)

class DragActivity(BaseActivity):
    _name = "drag"
    _title = "拖动"
    _icon = "bi-arrows-move"
    _category = "ui-automation"
    _scoped = False

    def __init__(self):
        super().__init__()
        self.arg_start: Union[None, tuple, UiComponent] = None
        self.arg_end: Union[None, tuple, UiComponent] = None
        self.arg_area: Union[None, UiComponent] = None
        self.properties["options"]["press_time"] = 1
        self.properties["options"]["drag_time"] = 1

    def _generate_title(self):
        return self._title

    def _generate_arg_start_html(self):
        start_html = ""
        if self.arg_start is None:
            start_html = '''<button class="btn btn-outline-secondary indicate-target-btn">Indicate Target</button>'''
        if isinstance(self.arg_start, UiComponent):
            start_html = f'''
            <div class="target-display">
                <img src="{self.arg_start.get_url()}" alt="Drag Start Element" class="target-image">
                <button class="btn btn-sm btn-outline-secondary indicate-target-btn" title="重新选择目标"><i class="bi bi-cursor"></i></button>
            </div>'''
        if isinstance(self.arg_start, tuple):
            start_html = f'''
                <div class="property-two-columns">
                    <div class="property-row">
                        <div class="property-label">x</div>
                        <div class="property-value">
                            <input type="text" class="form-control form-control-sm" value="{self.arg_start[0]}" placeholder="x">
                        </div>
                    </div>
                    <div class="property-row">
                        <div class="property-label">y</div>
                        <div class="property-value">
                            <input type="text" class="form-control form-control-sm" value="{self.arg_start[1]}" placeholder="y">
                        </div>
                    </div>
                </div>'''
        return start_html

    def _generate_arg_end_html(self):
        end_html = ""
        if self.arg_end is None:
            end_html = '''<button class="btn btn-outline-secondary indicate-target-btn">Indicate Target</button>'''
        elif isinstance(self.arg_end, UiComponent):
            end_html = f'''
            <div class="target-display">
                <img src="{self.arg_end.get_url()}" alt="Drag End Element" class="target-image">
                <button class="btn btn-sm btn-outline-secondary indicate-target-btn" title="重新选择目标"><i class="bi bi-cursor"></i></button>
            </div>'''
        else:
            end_html = f'''
                <div class="property-two-columns">
                    <div class="property-row">
                        <div class="property-label">x</div>
                        <div class="property-value">
                            <input type="text" class="form-control form-control-sm" value="{self.arg_end[0]}">
                        </div>
                    </div>
                    <div class="property-row">
                        <div class="property-label">y</div>
                        <div class="property-value">
                            <input type="text" class="form-control form-control-sm" value="{self.arg_end[1]}">
                        </div>
                    </div>
                </div>'''
        return end_html
    
    def _generate_arg_area_html(self):
        area_html = ""
        if self.arg_area is None:
            area_html = '''<button class="btn btn-outline-secondary indicate-target-btn">Indicate Target</button>'''
        elif isinstance(self.arg_area, UiComponent):
            area_html = f'''
            <div class="target-display">
                <img src="{self.arg_area.get_url()}" alt="Area Element" class="target-image">
                <button class="btn btn-sm btn-outline-secondary indicate-target-btn" title="重新选择区域"><i class="bi bi-cursor"></i></button>
            </div>'''
        return area_html
    
    def _generate_args_html(self):
        args_html = f"""
        <span>From:</span>
        {self._generate_arg_start_html()}
        <span>To:</span>
        {self._generate_arg_end_html()}
        <span>Inside:</span>
        {self._generate_arg_area_html()}
        """
        return args_html

    def to_dict(self):
        result = {
            "title": self._generate_title(),
            "args_html": self._generate_args_html(),
            "properties": self.properties,
        }
        result.update(super().to_dict())

        return result